﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BO.Admin;

namespace ProjectQuaDep.Admin.Banners
{
    public partial class Banner_Manager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showAll();
                showListBanner();
            }
        }
        protected void showAll()
        {
            string sSearch = txtSearch.Text.Trim();
            DataTable dt = new DataTable();
            dt = BannersBO.showAllBannerCache(sSearch);
            if (dt.Rows.Count > 0)
            {
                gvShow.DataSource = dt;
                gvShow.DataBind();
            }
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                cmdUpdate.Text = "Sửa chữa";
                pnUpdate.Visible = true;
                DataTable dt = new DataTable();
                dt = BannersBO.getInfomationByIDCache(ID);
                if (dt.Rows.Count > 0)
                {
                    lblID.Text = dt.Rows[0][BO.Const.tbBanners.id].ToString();
                    txtTitle.Text = dt.Rows[0][BO.Const.tbBanners.title].ToString();
                    txtDesc.Text = dt.Rows[0][BO.Const.tbBanners.shortContent].ToString();
                    txtImages.Text = dt.Rows[0][BO.Const.tbBanners.img].ToString();
                    txtSize.Text = dt.Rows[0][BO.Const.tbBanners.sizeBanner].ToString();
                    txtNote.Text = dt.Rows[0][BO.Const.tbBanners.note].ToString();
                    txtLink.Text = dt.Rows[0][BO.Const.tbBanners.url].ToString();
                    ddlType.SelectedValue = dt.Rows[0][BO.Const.tbBanners.catID].ToString();
                    //select
                    string sSelect = dt.Rows[0][BO.Const.tbBanners.status].ToString();
                    BO.Utils.setOneSelect(ckSelect, sSelect);
                }
            }
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Bạn đang ở trang: " + (gvShow.PageIndex + 1).ToString();
            showAll();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            pnUpdate.Visible = true;
            cmdUpdate.Text = "Thêm mới";
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            showAll();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (cmdUpdate.Text == "Thêm mới")
            {
                //them moi
                string kq = BannersBO.addBannerBO(txtTitle.Text, txtLink.Text, txtDesc.Text, txtImages.Text, BO.Utils.getOneSelect(ckSelect), txtSize.Text, ddlType.SelectedValue.ToString(), txtNote.Text);
                if (kq == "1")
                {
                    lblMs.Text = "";
                    pnUpdate.Visible = false;
                    showAll();
                }
                else
                {
                    lblMs.Text = "Thêm mới thất bại";
                }
            }
            else
            {
                //sua chua
                string kq = BannersBO.editBannerBO(txtTitle.Text, txtLink.Text, txtDesc.Text, txtImages.Text, BO.Utils.getOneSelect(ckSelect), txtSize.Text, ddlType.SelectedValue.ToString(), txtNote.Text, lblID.Text);
                if (kq == "1")
                {
                    lblMs.Text = "";
                    pnUpdate.Visible = false;
                    showAll();
                }
                else
                {
                    lblMs.Text = "Sửa chữa thất bại";
                }
            }
        }
        protected void showListBanner()
        {
            ddlType.DataSource = BannersBO.getListBannerCache();
            ddlType.DataTextField = "title";
            ddlType.DataValueField = "catid";
            ddlType.DataBind();
        }
        //
    }
}